<script type="text/javascript">
	var selectTagIds = {};
	var ids_ = '';
	var editType = 1;
	$(function(){
		$('#gtbeWord').keyup(function(){
			showSuggest();
		});
	});
	function gtbeAddTagToGoods(thisObj){
		var tag = $(thisObj).find('.gtbeHtml').html();
		var tagId = $(thisObj).find('.gtbeTagId').val();
		$('#gtbeSuggDiv').fadeOut(0).html('');
		pushTagId(tagId, tag);
		var h = '<span onclick="deleteTagId(\''+tagId+'\');" style="float:left;margin:3px;cursor:pointer;padding:4px;background-color:#87ADFA;font-size:13px;">'+tag+'</span>';
		$('#gtbeSelectedDiv').append(h);
	}
	function tagGoodsBatchEdit(n){
		editType = n;
		if(editType == 1){
			if($('#gtDiv1List').find('.goodsInfoDiv').length < 1){
				alert('没有商品信息！');
				return;
			}
		}else{
			ids_ = getTagsId();
			if(ids_.trim().length < 1){
				alert('请先选择查询标签，查询商品！');
				return;
			}
		}
		selectTagIds = {};
		$('#gtbeWord').val('');
		$('#gtbeSuggDiv,#gtbeSelectedDiv').html('');
		$('#gtbeSuggDiv').fadeOut(0);
		$('#gtBatchEdit1').showPop({'blockStyle':{'color':'#aaaaaa',opacity:0}});
	}
	
	function pushTagId(id, w){
		selectTagIds[id] = w;
	}
	function deleteTagId(id){
		var obj = $(arguments.callee.caller.arguments[0].target);
		obj.remove();
		selectTagIds[id] = null;
		delete selectTagIds[id];
	}
	function getDeltTagIds(){
		var h = '';
		for(var id in selectTagIds){
			h += id + ',';
		}
		return h;
	}
	
	function showSuggest(){
		var v = $('#gtbeWord').val();
		if(v == null || v.toString().trim().length < 1){
			$('#gtbeSuggDiv').html('').fadeOut(0);
			return;
		}
		startWith(v, $('#gtbeSelectedDiv'), function(rs){
			var h = '';
			if(rs && rs.length > 0){
				var list = eval(rs);
				for(var i = 0, len = list.length; i < len; i ++){
					var o = list[i];
					var id = o.id;
					var word = o.keyWord;
					h += '<div style="cursor:pointer;font-size:13px;background-color:#BAD0FC;margin-top:3px;width:100%px;padding-top:5px;padding-bottom:4px;text-align:center;" onclick="gtbeAddTagToGoods(this);">';
					h += '<span class="gtbeHtml">' + word+'</span><input type="hidden" class="gtbeTagId" value="'+id+'" /></div>';
				}
			}
			$('#gtbeSuggDiv').html(h).fadeIn(0);
		});
	}
	
	//批量执行
	function confirmBatchTags(){
		var url = 'editGoodsTags?';
		var ids = getDeltTagIds();
		$('#tagInfo_id').val(ids);
		if(ids.trim().length < 1){
			alert('请选择标签！');
			return;
		}
		url += 'deltIds=' + ids;
		
		var dataRange = $('input[name="dataRange"]').filter(':checked').val();
		if(!dataRange){
			alert('请选择数据范围！');
			return;
		}
		var cp = dataRange == 0 ? currentPage : dataRange;
		url += '&pp=' + tagObj.pp + '&cp=' + cp + '&ids=' + ids_;
		
		var dt = $('input[name="doType"]').filter(':checked').val();
		if(!dt){
			alert('请选择执行方式！');
			return;
		}
		url += '&type=' + dt;
		
		var liveDay = $('#liveDay').val();
		url += '&liveDay=' + liveDay;
		
		if(editType == 1){
			slf(1);
			url = 'editGoodsQuery?';
			var spObj = getSearchParam();
			spObj.cp = cp;
			url += 'spStr=' + jsonToString(spObj) + '&type=' + dt + '&deltIds=' + ids + '&liveDay=' + liveDay;
			
			ajax(url, function(rs){
				if(rs && rs.length > 10) {
					tagInfoBatch(rs);
				}else{
					$('#gtBatchEdit1').hidePop();
					queryGoods();
					slf();
					tagInfoBatch(rs);
				}
			});
		}else{
			ajax(url, function(rs){
				if(rs && rs.length > 10) {
					tagInfoBatch(rs);
				}else{
					$('#gtBatchEdit1').hidePop();
					queryByTagId();
					slf();
					tagInfoBatch(rs);
				}
			});
		}
	}
	//显示标签冲突信息
	function tagInfoBatch(html) {
		var tagObj = $('#tagInfoDiv');
		$('#tagInfo_name').html('待');
		if(html && html.length > 10) {
			$('#tagHTML').html(html);
			tagObj.showPop({'closeable':false, 'blockStyle' : {opacity:0}});
		}else{
			$('#tagHTML').html('');
			tagObj.hidePop();
		}
	}
	function slf(n){
		if(n && n > 0){
			$('#loadWaitingFloat').showPop({'blockStyle':{'opacity':0.1}, 'closeable':false, single:true});
		}else{
			$('#loadWaitingFloat').hidePop();
		}
	}
</script>

<div id="gtBatchEdit1" class="batchEditFloat">
	<div style="padding:15px;">
		<input type="text" style="padding-left:5px;width:440px;height:25px;" id="gtbeWord" />
		<div id="gtbeSuggDiv" style="display:none;position:absolute;margin-top:-1px;padding:5px;background-color:white;border:1px solid #dddddd;width:437px;">
			
		</div>
		<div style="margin-top:30px;padding:3px;">
			<div style="color:red;font-size:13px;">已选择标签：</div>
			<div id="gtbeSelectedDiv" style="font-size:13px;padding:5px;height:100px;">
				
			</div>
			<div>
				<table style="font-size:13px;">
					<tr>
						<td>
							有效天数：
						</td>
						<td>
							<input type="text" value="0" id="liveDay" onkeyup="this.value=this.value.replace(/[^\d]/gi,'');" /><span style="font-size:13px;color:gray;">有效天数为概数，0.5天的误差</span>
						</td>
					</tr>
					<tr>
						<td>
							数据范围：
						</td>
						<td>
							<input type="radio" name="dataRange" value="0" id="dataRange1" /><label for="dataRange1">符合条件的当前页</label>
							&nbsp;
							<input type="radio" name="dataRange" value="-1" id="dataRange2" /><label for="dataRange2">符合条件的所有商品</label>
						</td>
					</tr>
					<tr>
						<td>
							执行方式：
						</td>
						<td>
							<input type="radio" name="doType" value="1" id="doType1" /><label for="doType1">打上指定标签（若没有）</label>
							&nbsp;
							<input type="radio" name="doType" value="-1" id="doType2" /><label for="doType2">撤销指定标签（如果有）</label>
						</td>
					</tr>
					<tr>
						<td><br />
							<input type="button" value=" 确 定 " class="btnStyle1" onclick="confirmBatchTags();" />
						</td>
					</tr>
				</table>
			</div>
		</div>
	</div>
</div>

<div id="loadWaitingFloat">
	<div id="lwf_bg">
		
	</div>
</div>

<div id="tagInfoDiv">
	<div>
		<div style="font-size:13px;">在试图给以下商品打上名为 <span id="tagInfo_name"></span> 的标签时，发生冲突</div>
		<div style="font-size:13px;color:gray;">可能原因：以下商品已存在预定义标签</div>
		<input type="hidden" value="" id="tagInfo_id" />
		<input type="hidden" value="" id="tagInfo_liveday" />
	</div>
	<table>
		<tr>
			<td>
	<div id="tagHTML">
		
	</div></td>
			<td>
				<div style="font-size:13px;">
					点击“<span style="font-size:15px;color:red;font-weight:bold;">确定</span>”
					则继续打标签，点击“<span style="font-size:15px;color:red;font-weight:bold;">取消</span>”则不对以下商品打标签
				</div>
				<div style="padding-left:10px;padding-top:20px;">
					<input type="button" value=" 确 定 " class="btnStyle1" onclick="confirmToAdd1();" />
					<input type="button" value=" 取 消 " class="btnStyle1" onclick="cancelAddChongtu()" />
						
				</div>
			</td>
		</tr>
	</table>
</div>